<?php

namespace App\Trip\Plane\Interfaces\Result\Abstracts;

use App\Trip\Enums\Plane\PlaneProviders;
use Exception;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Str;

abstract class AbstractFlightItem implements Arrayable
{
    protected PlaneProviders $provider;
    protected array          $prefix
        = [
            1 => 'adult',
            2 => 'child',
            3 => 'baby',
        ];

    public function getProvider(): PlaneProviders
    {
        return $this->provider;
    }

    /**
     * @throws Exception
     */
    public function __toString(): string
    {
        return json_encode($this->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    }

    /**
     * @throws Exception
     */
    public function toArray(): array
    {
        $array          = [
            'name'              => $this->getName(),
            'name_uy'           => '',
            'tag'               => $this->dealTag(),
            'stock'             => $this->getStock(),
            'stocks'            => $this->getStocks(),
            'bookingParamKey'   => $this->bookingParamKey(),
            'policy_id'         => $this->getProviderExtensionId(),
            'discount'          => $this->getDiscount(),
            'cabin'             => $this->getCabin(),
            'cabin_name'        => $this->getCabinName(),
            'cabin_name_uy'     => $this->getCabinNameUy(),
            'check_prices'      => $this->getCheckPrices(),
            'construction_fees' => $this->getProviderConstructionFees(),
            'fuel_fees'         => $this->getProviderFuelFees(),
        ];
        $array          = array_merge($array, $this->getCheckPrices());
        $array          = array_merge($array, $this->getProviderConstructionFees());
        $array          = array_merge($array, $this->getProviderFuelFees());
        $array['price'] = $array['adult_price'];
        $this->cacheFlightItem();
        return $array;
    }

    public function getName(): string
    {
        return '';
    }

    private function dealTag()
    {
        $tag = $this->getTag();
        if (!$tag) {
            return __('plane/tag.蜗牛优选');
        }
        $afters = [
            "自营让利", "航司立减", "同退同改",
        ];
        if (Str::contains($tag, $afters)) {
            foreach ($afters as $after) {
                $adp = "（{$after}）";
                if (Str::contains($tag, $adp)) {
                    $res   = explode($adp, $tag);
                    $tag_0 = $res[0] ?? $tag;
                    $tag_1 = $after;
                    return __("plane/tag.{$tag_0}")."（".__("plane/tag.{$tag_1}")."）";
                }
            }
        }

        return __("plane/tag.{$tag}") ?: $tag;

    }

    public function getTag(): string
    {
        return '';
    }

    abstract public function getStock();

    abstract public function getStocks();

    abstract public function bookingParamKey();

    /**
     * 51book policy id  or qunar business ext
     * @return string
     */
    abstract public function getProviderExtensionId(): string;

    abstract public function getDiscount();

    abstract public function getCabin();

    abstract public function getCabinName(): string;

    public function getCabinNameUy(): string
    {
        return $this->getCabinName() ? __($this->getCabinName()) : "";
    }

    abstract public function getCheckPrices();

    abstract public function getProviderConstructionFees();

    abstract public function getProviderFuelFees();

    abstract protected function cacheFlightItem();

    abstract public function getFlight(): AbstractFlightQueryOneResult;

    abstract public function getChildCabin();

    abstract public function getBabyCabin();

    abstract public function getChildStock();

    abstract public function getProviderPrices();

}